﻿using System;
using System.Drawing;
using System.Reflection;
using System.Resources;
using CSharpHelpers.IOs;

namespace CSharpHelpers.Commons
{
    class ResourceHelper
    {
        /// <summary>
        /// 定义一个资源文件名 资源文件名 = 工程的默认命名空间+文件名(不带扩展名)
        /// </summary>
        private const string PublicResourceFileName = "AutoMemberCard.Properties.Resources";

        /// <summary>
        /// 从资源文件中读取一个资源 
        /// </summary>
        /// <param name="resName">要读取的资源名称</param>
        /// <returns>返回一个资源 读取失败返回NULL</returns>
        public object ReadFromResourceFile(String resName)
        {
            try
            {
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                var rm = new ResourceManager(PublicResourceFileName, myAssembly);
                object obj = rm.GetObject(resName);

                return obj;
            }
            catch (Exception e)
            {
                SystemLog.Error("讀取系統資源異常: " + e);
                return null;
            }
        }
        /// <summary>
        /// 获取资源图片
        /// 调用GetResourceImage方法即可。name为文件的名称不带有后缀.
        /// </summary>
        /// <param name="name">文件名</param>
        /// <returns>资源图片</returns>
        public Bitmap GetResourceImage(String name)
        {
            object tempbitmap = null;
            tempbitmap = ReadFromResourceFile(name);
            if (tempbitmap is Bitmap)
            {
                return (Bitmap)tempbitmap;
            }

            return null;
        }
    }
}
